<?php

function autoload_helper($class_name, $language_name = "")
{
	$language_suffix = $language_name ? "_" . $language_name : "";
	$dtf_include_needed = false;
	if (substr($class_name, -9, 9) == "_dtf_read")
	{
		$path_right_part = "/dt/dtf/" . substr($class_name, 0, -9) . $language_suffix . ".dtf.read.php";
		$dtf_include_needed = true;
	}
	elseif (substr($class_name, -10, 10) == "_dtf_write")
	{
		$path_right_part = "/dt/dtf/" . substr($class_name, 0, -10) . $language_suffix . ".dtf.write.php";
		$dtf_include_needed = true;
	}
	elseif (substr($class_name, -4, 4) == "_dtf")
	{
		$path_right_part = "/dt/dtf/" . substr($class_name, 0, -4) . $language_suffix . ".dtf.php";
		$dtf_include_needed = true;
	}
	elseif (substr($class_name, -3, 3) == "_dt")
	{
		$path_right_part = "/" . substr($class_name, 0, -3) . $language_suffix . ".dt.php";
		if (file_exists(PATH_DT . $path_right_part))
		{
			require_once(PATH_DT . $path_right_part);
			return true;
		}
		elseif (!$language_name or file_exists(PATH_DT_COMMON . $path_right_part))
		{
			require_once(PATH_DT_COMMON . $path_right_part);
			return true;
		}
		else
		{
			return false;
		}
	}
	if ($dtf_include_needed)
	{
		if (file_exists(PATH_BIN_COMMON . $path_right_part))
		{
			require_once(PATH_BIN_COMMON . $path_right_part);
			return true;
		}
		elseif (!$language_name or file_exists(PATH_BIN_CORE . $path_right_part))
		{
			require_once(PATH_BIN_CORE . $path_right_part);
			return true;
		}
		else
		{
			return false;

		}
	}
}

function __autoload($class_name)
{
	global $LANGUAGE_NAME;
	if (isset($LANGUAGE_NAME))
	{
		if (!autoload_helper($class_name, $LANGUAGE_NAME))
		{
			autoload_helper($class_name);
		}
	}
	else
	{
		autoload_helper($class_name);
	}
}

require_once(PATH_BIN_CORE . "/auxil.php");
require_once(PATH_BIN_CORE . "/error.php");
require_once(PATH_BIN_CORE . "/request.php");
require_once(PATH_BIN_CORE . "/output_buffer.php");
require_once(PATH_BIN_CORE . "/response.php");
require_once(PATH_BIN_CORE . "/db.php");
require_once(PATH_BIN_CORE . "/auth.php");
require_once(PATH_BIN_CORE . "/xnode.php");
require_once(PATH_BIN_CORE . "/xdom.php");
require_once(PATH_BIN_CORE . "/read.loader.php");
require_once(PATH_BIN_CORE . "/base.read.php");
require_once(PATH_BIN_CORE . "/write.loader.php");
require_once(PATH_BIN_CORE . "/base.write.php");
require_once(PATH_BIN_CORE . "/pass_info.php");
require_once(PATH_BIN_CORE . "/xslt.php");

ignore_user_abort(true);

//=== error ===//

require_once("bin/error.php");
$error = new error(PATH_CORE, PATH_HOME, PATH_BIN_CORE . "/error.xslt", "UTF-8", $config["debug_ip"]);

//=== localization: PART 1 ===//

setlocale(LC_ALL, "ru_RU.UTF-8", "rus_rus");
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
date_default_timezone_set("Europe/Moscow");

//=== request ===//

$request = new request(PATH_HTTP_HOME, true, false, array("?", "?xml", "?xsl", "?xslt", "?sql"));

if ($request->is_redirect_needed())
{
	response::redirect_permanently($request->get_canonical_path());
}

//=== db ===//

$db = new db($config["db_host"], $config["db_user"], $config["db_pass"], $config["db_name"], isset($config["db_time_zone"]) ? $config["db_time_zone"] : "");
if (isset($_GET["sql"]))
{
	$db->set_debug_mode(true);
}

//=== language_loader ===//

$language_loader = null;

if (!isset($config["disable_languages_and_sections"]) or !$config["disable_languages_and_sections"])
{
	require_once(PATH_BIN_CORE . "/language_loader.php");

	$language_cache_file = PATH_CACHE . "/language/language.data";
	if (file_exists($language_cache_file))
	{
		$language_loader = unserialize(file_get_contents_safe($language_cache_file));
	}
	else
	{
		$language_loader = new language_loader();
		file_put_contents_safe($language_cache_file, serialize($language_loader));
	}
	$ok = $language_loader->define_current_language($request->get_host());
	if (!$ok)
	{
		finalize();
		die();
	}
	if ($language_loader->is_redirect_needed())
	{
		response::redirect_permanently($language_loader->get_canonical_url());
		finalize();
		die();
	}
}

//=== x_session ===//

if (file_exists(PATH_BIN . "/session_init.inc.php"))
{
	include(PATH_BIN . "/session_init.inc.php");
}
else
{
	require_once(PATH_LIB_CORE . "/x_session/x_session.php");
	$session = new x_session(PATH_SESSIONS, null, $request->get_prefix() . "/");
}

//=== auth ===//

$auth = new auth($db, $session);

//=== modules loading ===//

$redirect_url = "";

if (isset($_REQUEST["write"]))
{
	$module_name = $_REQUEST["write"];
	$session->check_session_is_created();
	$write_loader = new write_loader($module_name);
	$ok = $write_loader->run();
	if (!$ok or !($redirect_url = $write_loader->get_redirect_url()))
	{
		$redirect_url = $request->get_full_prefix() . "/";
	}
}
else
{
	$is_main_section = false;
	$module_name = $request->get_folders(1);
	if (!$module_name)
	{
		if ($auth->get_user_id())
		{
			$module_name = $config["default_module_name"];
		}
		else
		{
			$module_name = $config["default_module_name_for_guest"];
		}
		$is_main_section = true;
	}
	$read_loader = new read_loader($module_name, $is_main_section, $config["inherited_modules"], $config["static_prefix"]);
	$read_loader->run();
	$redirect_url = $read_loader->get_redirect_url();
}

if (!$error->is_error_occured())
{
	if ($redirect_url)
	{
		output_buffer::clean_content();
		response::redirect_simply($redirect_url);
	}
	else
	{
		if ($read_loader->is_error_403_occured())
		{
			response::set_error_403();
			$read_loader->run_error_403();
		}
		elseif ($read_loader->is_error_404())
		{
			response::set_error_404();
			$read_loader->run_error_404();
		}

		output_buffer::set_header("Expires", "Mon, 26 Jul 1997 00:00:00 GMT");
		output_buffer::set_header("Cache-Control", "no-cache, must-revalidate");
		output_buffer::set_header("Pragma", "no-cache");

		$output_is_done = false;

		if ($error->is_dev_mode())
		{
			if ($request->get_params_string() == "?" or isset($_GET["xml"]))
			{
				response::write_xml($read_loader->get_xml()->get_xml());
				$output_is_done = true;
			}
			elseif ($request->get_params_string() == "?xslt" or $request->get_params_string() == "?xsl")
			{
				response::write_xml($read_loader->get_xslt());
				$output_is_done = true;
			}
			elseif ($request->get_params_string() == "?sql" or isset($_GET["sql"]))
			{
				$html = "<html><head><title>SQL Debugger</title><body bgcolor='white'><style type='text/css'> TABLE, TD, TR, BODY { font-family: verdana,arial, sans-serif;color:black;font-size:11px }</style>";
				$html .= "<h1 align='center'>SQL Total Time: " . $db->debug_get_sql_time() . " for " . $db->debug_get_query_count() . " queries</h1><br />" . $db->debug_get_html();
				$html .= "<br /><div align='center'><strong>Total SQL Time: " . $db->debug_get_sql_time(). "</div></body></html>";
				response::write_html($html);
				$output_is_done = true;
			}
		}

		$output_is_done = $output_is_done || output_buffer::content_exists();

		if (!$output_is_done)
		{
			$xslt_functions = null;
			if (file_exists(PATH_BIN . "/xslt_functions.php"))
			{
				require_once(PATH_BIN . "/xslt_functions.php");
				$xslt_functions = get_xslt_functions_list();
			}
			if ($language_loader)
			{
				$xslt_functions[] = "lang";
			}
			response::write_html($read_loader->get_xml()->xslt_process($read_loader->get_xslt(), $xslt_functions));
		}
	}
}

finalize();

function finalize()
{
	global $error;

	if ($error->is_error_occured())
	{
		// logging
		if ($_SERVER['REMOTE_ADDR'] != "127.0.0.1")
		{
			$date_suffix = date("Y-m-d_H-i-s");
			$file = PATH_LOG . "/errors/error_{$date_suffix}.html";
			file_put_contents($file, $error->get_errors_html());
		}
		// response
		response::set_error_500();
		if ($error->is_dev_mode())
		{
			response::write_html($error->get_errors_html());
		}
		else
		{
			response::write_html("<h1>Ошибка 500</h1><p>Извините, на сервере произошла ошибка. Разработчики уже получили соответствующее уведомление. Попробуйте выполнить действие ещё раз. Если не получилось &#8212; наберитесь терпения и зайдите, пожалуйста, попозже.</p>
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			<!--1231234123412341234kf asuf oayudf qweyfr qreoufqoeurf qufg qrfq rwfq rfqrf qruf qrwfq rwfqrwf-->
			");
		}
	}

	output_buffer::finalize();
}

?>